package com.jinghao.tws.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.jinghao.core.common.exception.JingHaoCustomerException;
import com.jinghao.core.common.service.impl.JingHaoBaseServiceImpl;
import com.jinghao.core.common.util.JhMD5Util;
import com.jinghao.core.common.util.cnd.JhCnd;
import com.jinghao.core.common.util.cnd.JhUpdate;
import com.jinghao.sys.entity.SysAccount;
import com.jinghao.sys.entity.SysAccountOrganization;
import com.jinghao.sys.entity.SysAccountRoleOrganization;
import com.jinghao.sys.entity.SysAccount.ESysAccount;
import com.jinghao.sys.service.ISysAccountOrganizationService;
import com.jinghao.sys.service.ISysAccountRoleOrganizationService;
import com.jinghao.sys.util.EJhSysCode;
import com.jinghao.tws.service.IWvPublicSysService;

@Service("wvPublicSysService")
public class WvPublicSysServiceImpl extends JingHaoBaseServiceImpl implements IWvPublicSysService {

	//==================定制内容开始======================
	@Resource
	private ISysAccountRoleOrganizationService sysAccountRoleOrganizationService;
	
	@Resource
	private ISysAccountOrganizationService  sysAccountOrganizationService;
//在这里修改代码
	@Override
	public void addSysAccount(SysAccount model) {
		this.checkSysAccount(model);
		
		Map<String,Object> map=model.getObjMap();
		if(map.get("jhactPassword")==null||StringUtils.isEmpty(map.get("jhactPassword").toString())){
			model.setJhactPassword(JhMD5Util.DEFAULT_MD5);
		}else{
			model.setJhactPassword(map.get("jhactPassword").toString());
		}
		if(map.get("jhactStatus")==null||StringUtils.isEmpty(map.get("jhactStatus").toString())){
			model.setJhactStatus(ESysAccount.jhactStatus_1.getKey());
		}else{
			model.setJhactStatus(map.get("jhactStatus").toString());
		}
		if(map.get("jhactOrd")==null||StringUtils.isEmpty(map.get("jhactOrd").toString())){
			model.setJhactOrd(1);
		}else{
			model.setJhactOrd(Integer.parseInt(map.get("jhactOrd").toString()));
		}
		if(map.get("jhactCrorgUuid")==null||StringUtils.isEmpty(map.get("jhactCrorgUuid").toString())){
			model.setJhactCrorgUuid("  ");
		}else{
			model.setJhactCrorgUuid(map.get("jhactCrorgUuid").toString());
		}	
		if(map.get("jhactCractCreaterUuid")==null||StringUtils.isEmpty(map.get("jhactCractCreaterUuid").toString())){
			model.setJhactCractCreaterUuid("  ");
		}else{
			model.setJhactCractCreaterUuid(map.get("jhactCractCreaterUuid").toString());
		}
		List<String> _orgList=null;
		if(map.get("jhactCrorgCollectionUuid")==null||StringUtils.isEmpty(map.get("jhactCrorgCollectionUuid").toString())){
			model.setJhactCrorgCollectionUuid("");
		}else{
			_orgList=(List<String>)map.get("jhactCrorgCollectionUuid");
			String _stringList="";
			for(String strMsg:_orgList){
				if(strMsg!=null&&!strMsg.trim().equals("")){
					if(_stringList.equals("")){
						_stringList=strMsg;
					}else{
						_stringList=_stringList+","+strMsg;
					}
				}
			}
			model.setJhactCrorgCollectionUuid(_stringList);
		}
		
		if(map.get("jhactNature")==null||StringUtils.isEmpty(map.get("jhactNature").toString())){
			model.setJhactNature(ESysAccount.jhactNature_1.getKey());
		}else{
			model.setJhactNature(map.get("jhactNature").toString());
		}
		model.setJhactType(ESysAccount.jhactType_1.getKey());
		
		model.setJhactCdate(new Date());
		model.setJhactUuid(model.getUUID());
		JhCnd cnd1=new JhCnd();
		cnd1.append(" and (JHACT_CODE = '"+model.getJhactCode()+"' or JHACT_NAME = '"+model.getJhactName()+"')");
		model.lnkCnd(cnd1);
		List<Map<String,Object>> list =this.queryMapListById(model, "queryMapListByPassword");
		if(list.size()>0){
			Map<String,Object> tempMap=list.get(0);
			if(tempMap.get("jhactCode")!=null&&tempMap.get("jhactCode").toString().equals(model.getJhactCode())){
				throw new JingHaoCustomerException(EJhSysCode.SYS4001.getCode(),"该账户已经存在，请重新输入用户名！");
			}
			if(tempMap.get("jhactName")!=null&&tempMap.get("jhactName").toString().equals(model.getJhactName())){
				throw new JingHaoCustomerException(EJhSysCode.SYS4001.getCode(),"该账户已经存在，请重新输入全称！");
			}
			throw new JingHaoCustomerException(EJhSysCode.SYS4006);
		}else{
			
			int nub=this.add(model);
			SysAccountOrganization sysModel=null;
			//保存账户与机构关系信息
			if(model.getJhactCrorgUuid()!=null&&!model.getJhactCractCreaterUuid().trim().equals("")){
				sysModel=new SysAccountOrganization();
				sysModel.setJhamoJhactUuid(model.getJhactUuid());
				sysModel.setJhamoJhorgUuid(model.getJhactCractCreaterUuid());
				this.sysAccountOrganizationService.add(sysModel);
			}
			//保存多个机构情况
			if(_orgList!=null&&_orgList.size()>0){
				for(String orgUuid:_orgList){
					if(orgUuid!=model.getJhactCrorgUuid()){
						sysModel=new SysAccountOrganization();
						sysModel.setJhamoJhactUuid(model.getJhactUuid());
						sysModel.setJhamoJhorgUuid(orgUuid);
						this.sysAccountOrganizationService.add(sysModel);
					}
				}
			}
			
			//保存角色
	        if(map.get("roles")!=null){
	        	List<String> roles=(List<String>)map.get("roles");
	        	SysAccountRoleOrganization newModel=null;
	        	for(String _uuid:roles){
	        		newModel=new SysAccountRoleOrganization();
	        		newModel.setJhamrJhactUuid(model.getJhactUuid());
	        		newModel.setJhamrJhrolUuid(_uuid);
	        		this.sysAccountRoleOrganizationService.add(newModel);
	        	}
	        }
		}
		
	}

	public void checkSysAccount(SysAccount model){
		Map<String,Object> map=model.getObjMap();
		if(map==null){
			throw new JingHaoCustomerException(EJhSysCode.SYS4000.getCode(),"请输入账户信息");
		}
		if(map.get("jhactCode")==null||StringUtils.isEmpty(map.get("jhactCode").toString())){
			throw new JingHaoCustomerException(EJhSysCode.SYS4001.getCode(),"请输入账户名");
		}
		model.setJhactCode(map.get("jhactCode").toString());
		
		if(map.get("jhactName")==null||StringUtils.isEmpty(map.get("jhactName").toString())){
			throw new JingHaoCustomerException(EJhSysCode.SYS4001.getCode(),"请输入全称");
		}
		model.setJhactName(map.get("jhactName").toString());
		
		if(map.get("jhactMobile")!=null&&!StringUtils.isEmpty(map.get("jhactMobile").toString())){
			model.setJhactMobile(map.get("jhactMobile").toString());
		}
		if(map.get("jhactEmail")!=null&&!StringUtils.isEmpty(map.get("jhactEmail").toString())){
			model.setJhactEmail(map.get("jhactEmail").toString());
		}
		if(map.get("jhactMemo")!=null&&!StringUtils.isEmpty(map.get("jhactMemo").toString())){
			model.setJhactMemo(map.get("jhactMemo").toString());
		}
	}
	/**
	 * 修改用户信息
	 */
	@Override
	public void updateSysAccount(SysAccount model) {
		this.checkSysAccount(model);
		Map<String,Object> map=model.getObjMap();
		if (map.get("jhactUuid") == null || StringUtils.isEmpty(map.get("jhactUuid").toString())) {
			throw new JingHaoCustomerException(EJhSysCode.SYS4001.getCode(), "请选择要操作的账户");
		}
		if(map.get("jhactNature")==null||StringUtils.isEmpty(map.get("jhactNature").toString())){
			model.setJhactNature(ESysAccount.jhactNature_1.getKey());
		}else{
			model.setJhactNature(map.get("jhactNature").toString());
		}
		if(map.get("jhactCrorgUuid")==null||StringUtils.isEmpty(map.get("jhactCrorgUuid").toString())){
			  model.setJhactCrorgUuid("  ");
		}else{
			  model.setJhactCrorgUuid(map.get("jhactCrorgUuid").toString());
		}	
		
		List<String> _orgList=null;
		if(map.get("jhactCrorgCollectionUuid")==null||StringUtils.isEmpty(map.get("jhactCrorgCollectionUuid").toString())){
			model.setJhactCrorgCollectionUuid("");
		}else{
			_orgList=(List<String>)map.get("jhactCrorgCollectionUuid");
			String _stringList="";
			for(String strMsg:_orgList){
				if(strMsg!=null&&!strMsg.trim().equals("")){
					if(_stringList.equals("")){
						_stringList=strMsg;
					}else{
						_stringList=_stringList+","+strMsg;
					}
				}
			}
			model.setJhactCrorgCollectionUuid(_stringList);
		}
		
		model.setJhactUuid(map.get("jhactUuid").toString());
		model.setJhactUdate(new Date());
		JhCnd cnd1=new JhCnd();
		cnd1.andNotEquals("jhactUuid");
		cnd1.append(" and (JHACT_CODE = '"+model.getJhactCode()+"' or JHACT_NAME = '"+model.getJhactName()+"') ");
		model.lnkCnd(cnd1);
		List<Map<String,Object>> list =this.queryMapListById(model, "queryMapListByPassword");
		if(list.size()>=1){
			Map<String,Object> tempMap=list.get(0);
			if(tempMap.get("jhactCode")!=null&&tempMap.get("jhactCode").toString().equals(model.getJhactCode())){
				throw new JingHaoCustomerException(EJhSysCode.SYS4001.getCode(),"该账户已经存在，请重新输入用户名！");
			}
			if(tempMap.get("jhactName")!=null&&tempMap.get("jhactName").toString().equals(model.getJhactName())){
				throw new JingHaoCustomerException(EJhSysCode.SYS4001.getCode(),"该账户已经存在，请重新输入全称！");
			}
		} 
		//信息正确,修改数据
		JhUpdate update=new JhUpdate(); 
		if(model.getJhactCrorgUuid()!=null&&!model.getJhactCrorgUuid().trim().equals("")){
			update.add("jhactCode").add("jhactName").add("jhactMobile").add("jhactEmail").add("jhactMemo").add("jhactUdate").add("jhactCrorgCollectionUuid").add("jhactNature").add("jhactCrorgUuid");
		}else{
			update.add("jhactCode").add("jhactName").add("jhactMobile").add("jhactEmail").add("jhactMemo").add("jhactUdate").add("jhactCrorgCollectionUuid").add("jhactNature");
		}
		model.setJhUpdate(update);
		JhCnd cnd=new JhCnd();
		cnd.andEquals("jhactUuid");
		model.lnkCnd(cnd);
		this.updateSame(model);
		//删除用户与机构有关系
		SysAccountOrganization sysModel=new SysAccountOrganization();
		sysModel.setJhamoJhactUuid(model.getJhactUuid());
		cnd.reset();
		cnd.andEquals("jhamoJhactUuid");
		sysModel.setJhCnd(cnd);
		this.sysAccountOrganizationService.deleteSame(sysModel);
		//保存账户与机构关系信息
		if(model.getJhactCrorgUuid()!=null&&!model.getJhactCrorgUuid().trim().equals("")){
			sysModel.setJhamoJhactUuid(model.getJhactUuid());
			sysModel.setJhamoJhorgUuid(model.getJhactCrorgUuid());
			this.sysAccountOrganizationService.add(sysModel);
		}
		
		//保存多个机构情况
		if(_orgList!=null&&_orgList.size()>0){
			for(String orgUuid:_orgList){
				if(orgUuid!=model.getJhactCrorgUuid()){
					sysModel=new SysAccountOrganization();
					sysModel.setJhamoJhactUuid(model.getJhactUuid());
					sysModel.setJhamoJhorgUuid(orgUuid);
					this.sysAccountOrganizationService.add(sysModel);
				}
			}
		}
		
		//删除用户关联角色
		SysAccountRoleOrganization tempModel=new SysAccountRoleOrganization();
		cnd.reset();
		cnd.andEquals("jhamrJhactUuid");
		tempModel.lnkCnd(cnd);
		tempModel.setJhamrJhactUuid(model.getJhactUuid());
		this.sysAccountRoleOrganizationService.deleteSame(tempModel);
		//保存角色
        if(map.get("roles")!=null){
        	List<String> roles=(List<String>)map.get("roles");
        	SysAccountRoleOrganization newModel=null;
        	for(String _uuid:roles){
        		newModel=new SysAccountRoleOrganization();
        		newModel.setJhamrJhactUuid(model.getJhactUuid());
        		newModel.setJhamrJhrolUuid(_uuid);
        		this.sysAccountRoleOrganizationService.add(newModel);
        	}
        }
	}
	
	//==================定制内容结束======================
}
